package com.cy.model;

import com.alibaba.druid.util.StringUtils;
import com.cy.BasicModel;
import com.cy.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressWarnings("serial")
@TableBind(tableName="cy_order",pkName = "order_id")
public class Order extends BasicModel<Order> {
    public static final Order dao = new Order();
    public static final String SELECT_ALL = "cy_order.`order_id`,`order_no`,`original_order_no`,`express_no`,`user_name`,`product_no`,`count`,`shop_name`,`phone`,`region_id`,cy_order.`address`,cy_order.`user_id`,`license_token_id`,cy_order.`create_time` ";

    public Page<Order> paginate(Integer pageNumber, Integer pageSize, String column, Order model) {
        List<Object> paras = new ArrayList<>();
        StringBuilder sqlExceptSelectBuffer = new StringBuilder("FROM " + getTableName() + " WHERE 1=1");
        StringBuilder selectBuffer = new StringBuilder("SELECT ").append(column);
        if (pageNumber == null || pageNumber == 0) {
            pageNumber = 1;
        } else {
            pageNumber++;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }

        if (model != null) {
            String[] attrNames = model.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = model.get(attrName);
                if (attrValue != null) {
                    if("start".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND create_time >= ?");
                    }else if("end".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND create_time <= ?");
                    }else if("order_no".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND order_no LIKE ?");
                        attrValue = "%"+attrValue+"%";
                    }else if("original_order_no".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND original_order_no LIKE ?");
                        attrValue = "%"+attrValue+"%";
                    }else if("express_no".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND express_no LIKE ?");
                        attrValue = "%"+attrValue+"%";
                    }else if("user_name".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND user_name LIKE ?");
                        attrValue = "%"+attrValue+"%";
                    }else if("product_no".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND product_no LIKE ?");
                        attrValue = "%"+attrValue+"%";
                    }else if("shop_name".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND shop_name LIKE ?");
                        attrValue = "%"+attrValue+"%";
                    }else if("phone".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND phone LIKE ?");
                        attrValue = "%"+attrValue+"%";
                    }else if("address".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND address LIKE ?");
                        attrValue = "%"+attrValue+"%";
                    }else{
                        sqlExceptSelectBuffer.append(" AND `" + attrName + "`=?");
                    }
                    paras.add(attrValue);
                }
            }
        }

        sqlExceptSelectBuffer.append(" ORDER BY `create_time` DESC ");
        if (paras.size() > 0) {
            return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString(), paras.toArray());
        }
        return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString());
    }

    public Page<Order> findOrderAndTokenList(Integer pageNumber, Integer pageSize,String from_Where, Order order) {
        List<Object> paras = new ArrayList<>();
        StringBuffer sqlExceptSelectBuffer = new StringBuffer("FROM " + getTableName());
        if (!StringUtils.isEmpty(from_Where)){
            sqlExceptSelectBuffer.append(from_Where);
        }else {
            sqlExceptSelectBuffer.append(" ,user u  where cy_order.user_id = u.user_id ");
        }
        StringBuffer selectBuffer = new StringBuffer("SELECT ").append(SELECT_ALL).append(", u.status user_status ,u.share_code,u.mobile user_mobile ");
        if (pageNumber == null || pageNumber == 0) {
            pageNumber = 1;
        }else{
            pageNumber++;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }

        if (order != null) {
            String[] attrNames = order.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = order.get(attrName);
                if (attrValue != null) {
                    if("start".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND cy_order.`create_time` > DATE_FORMAT(?,'%Y-%m-%d %T')");
                    }else if("end".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND cy_order.`create_time` < DATE_FORMAT(?,'%Y-%m-%d %T')");
                    }else if("token_id".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND lt.token_id = ?");
                    }else if("token_code".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND lt.license_token LIKE ?");
                        attrValue = "%"+attrValue+"%";
                    }else{
                        sqlExceptSelectBuffer.append(" AND `" + attrName + "`=?");
                    }
                    paras.add(attrValue);
                }
            }
        }

            Page<Order> orderPage = null;
        if (paras.size() > 0){
            orderPage = paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString(), paras.toArray());
        }else{
            orderPage = paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString());
        }
        List<Order> orderList = orderPage.getList();
        for(Order o : orderList){
            List<LicenseToken> ltList = LicenseToken.dao.findByOrder(o.getOrderId());
            o.put("tokens",ltList);
        }
        return orderPage;
    }

    public List<Order> findOrderInfoByExpress(Order order,String select,String from_Where){
        List<Object> paras = new ArrayList<>();

        StringBuffer selectBuffer = new StringBuffer("SELECT ");
        if(!StringUtils.isEmpty(select)){
            selectBuffer.append(select);
        }else {
            selectBuffer.append(SELECT_ALL);
        }

        StringBuffer sqlExceptSelectBuffer = new StringBuffer(" FROM " + getTableName());

        if (!StringUtils.isEmpty(from_Where)){
            sqlExceptSelectBuffer.append(from_Where);
        }else {
            sqlExceptSelectBuffer.append(" where 1=1 ");
        }

        if (order != null) {
            String[] attrNames = order.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = order.get(attrName);
                if (attrValue != null) {
                    sqlExceptSelectBuffer.append(" AND `" + attrName + "`=?");
                    paras.add(attrValue);
                }
            }
        }
        selectBuffer.append(sqlExceptSelectBuffer);

        return find(selectBuffer.toString(),paras.toArray());
    }

    public Order findByOrderNo(String orderNo){
        return findFirst("select * from " +getTableName()+ " where order_no = ?" , orderNo);
    }

    public Order findByOrderNoAndProductNo(String orderNo, String productNo){
        return findFirst("select * from " +getTableName()+ " where order_no = ? and product_no = ?" , orderNo, productNo);
    }
    
    public List<Order> findByExpressNo(String expressNo) {
    	return find("SELECT product_no, count FROM cy_order WHERE express_no = ?", expressNo);
    }

    public List<Order> findByOriginalOrderNo(String originalOrderNo){
        return find("select * from " + getTableName() + " where original_order_no = ?", originalOrderNo);
    }

    public Order setOrderId(Long orderId){
        this.set("order_id",orderId);
        return this;
    }

    public Long getOrderId(){
        return this.get("order_id");
    }

    public Order setOrderNo(String orderNo){
        this.set("order_no",orderNo);
        return this;
    }

    public String getOrderNo(){
        return this.getStr("order_no");
    }

    public Order setOriginalOrderNo(String originalOrderNo){
        this.set("original_order_no",originalOrderNo);
        return this;
    }

    public String getOriginalOrderNo(){
        return this.getStr("original_order_no");
    }

    public Order setExpressNo(String expressNo){
        this.set("express_no",expressNo);
        return this;
    }

    public String getExpressNo(){
        return this.getStr("express_no");
    }

    public Order setUserName(String userName){
        this.set("user_name",userName);
        return this;
    }

    public String getUserName(){
        return this.getStr("user_name");
    }

    public Order setProductNo(String productNo){
        this.set("product_no",productNo);
        return this;
    }

    public String getProductNo(){
        return this.getStr("product_no");
    }

    public Order setCount (Integer count){
        this.set("count",count);
        return this;
    }

    public Integer getCount(){
        return this. get("count");
    }

    public Order setShopName(String shopName){
        this.set("shop_name",shopName);
        return this;
    }

    public String getShopName(){
        return this.getStr("shop_name");
    }

    public Order setPhone(String phone){
        this.set("phone",phone);
        return this;
    }

    public String getPhone(){
        return this.getStr("phone");
    }
    
    public Order setRegionId (Long regionId){
        this.set("region_id",regionId);
        return this;
    }

    public Integer getRegionId(){
        return this. get("region_id");
    }

    public Order setAddress(String address){
        this.set("address",address);
        return this;
    }

    public String getAddress(){
        return this.getStr("address");
    }

      public Order setUserId(Long userId){
        this.set("user_id",userId);
        return this;
    }

    public Long getUserId(){
        return this. get("user_id");
    }


    public Order setCreateTime(Date createTime){
        this.set("create_time",createTime);
        return this;
    }

    public Date getCreateTime(){
        return this.getDate("create_time");
    }
}
